<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <el-form :inline="true" :model="searchMap.data" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.goodsName"
          placeholder="商品名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="addGoods()">添加商品</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格列表 -->
    <el-table :data="goodsList" tooltip-effect="dark" style="width: 100%">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="goodsName" label="商品名称" width="120">
      </el-table-column>
      <el-table-column prop="brandId" label="品牌" width="120">
      </el-table-column>
      <el-table-column prop="caption" label="副标题" width="120">
      </el-table-column>
      <el-table-column
        prop="price"
        label="商品价格"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="smallPic" label="小图" show-overflow-tooltip>
        <template slot-scope="scope">
          <div>
            <img
              v-for="(item, index) in scope.row.smallPic"
              :src="item"
              width="100px"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="typeTemplateId"
        label="分类模板"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="auditStatus" label="状态" show-overflow-tooltip>
        <template slot-scope="scope">
          <p v-if="scope.row.auditStatus == 0">通过</p>
          <p v-if="scope.row.auditStatus == 1">驳回</p>
          <p v-if="scope.row.auditStatus == 2">申请中</p>
          <p v-if="scope.row.auditStatus == 3">其他</p>
          <p v-if="scope.row.auditStatus == 4">其他</p>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="280">
        <template slot-scope="scope">
          <el-button
            type="warning"
            icon="el-icon-delete"
            @click="delOne(scope.row)"
            >删除</el-button
          >
          <el-button type="primary" icon="el-icon-edit" @click=""
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        style="float: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchMap.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { search, delOne } from "@/api/goods";
export default {
  data() {
    return {
      goodsList: [],
      searchMap: {
        page: 1,
        limit: 5,
        data: {},
      },
      total: 0,
      goods: {},
    };
  },
  methods: {
    //搜索
    searchData() {
      var _this = this;
      this.searchMap.data.sellerId = sessionStorage.getItem("token");
      search(this.searchMap).then((res) => {
        console.log(res);
        res.data.forEach((element) => {
          element.smallPic = eval(element.smallPic);
        });
        _this.goodsList = res.data;
        _this.total = res.total;
      });
    },
    addGoods() {
      this.$router.push("/goods/add");
    },
    //删除单个
    delOne(row) {
      this.$confirm("此操作将永久删除文案, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delOne({"id":row.id}).then((res) => {
          if (res.success) {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            this.searchData();
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        });
      });
    },

    //每页多少条
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.searchData();
    },
    //第几页
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.searchData();
    },
  },
  created() {
    this.searchData();
  },
};
</script>